/**
 * 
 */
package gemini.pollux.ui.client.utils.widgets.dialog;

import gemini.pollux.ui.client.utils.widgets.dialog.GmnDialogEvent.DialogActionType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class is used to show an confirmation dialog for user.<br/>
 * Implement <code>onOK</code> and <code>onCancel</code> methods.
 */
public class GmnDialog extends Composite implements HasHandlers {

	private static final String DEFAULT_WIDTH = "325px";
	private static final String DEFAULT_HEIGHT = "130px";
	
	private String title;
	private String message;
	
	private Button btnOK = new Button();
	private Button btnCancel = new Button();
	private Image imgIcon = new Image();
	
	private DialogBox dialog = new DialogBox();
	private Widget widget;
	
	private GmnDialogType dialogType;
	
	public enum GmnDialogType {
		WARNING,
		ERROR,
		INFO,
		WIDGET, CONFIRM;
	}
	
	public GmnDialog(GmnDialogType dialogType, String title, String message, 
			String okText) {
		this.dialogType = dialogType;
		this.title = title;
		this.setMessage(message);
		btnOK.setHTML(okText);
	}
	
	public GmnDialog(String title, String message, 
			String okText, String cancelText) {
		this.dialogType = GmnDialogType.CONFIRM;
		this.title = title;
		this.setMessage(message);
		btnOK.setHTML(okText);
		btnCancel.setHTML(cancelText);
	}
	
	public GmnDialog(String title, String message, 
			String okText) {
		this.dialogType = GmnDialogType.INFO;
		this.title = title;
		this.setMessage(message);
		btnOK.setHTML(okText);
	}
	
	public GmnDialog(String message, 
			String okText) {
		this.dialogType = GmnDialogType.INFO;
		this.setMessage(message);
		btnOK.setHTML(okText);
	}
	
	public GmnDialog(String title, Widget widget, 
			String okText) {
		this.dialogType = GmnDialogType.WIDGET;
		this.title = title;
		this.widget = widget;
		btnOK.setHTML(okText);
	}
	
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	public HandlerRegistration addConfirmationHandler(GmnDialogHandler handler) {
		return addHandler(handler, GmnDialogEvent.getType());
	}
	
	public void show(){
		if(dialogType.equals(GmnDialogType.ERROR)){
			imgIcon = new Image("images/icons/ico_error.png");
		} else if(dialogType.equals(GmnDialogType.INFO)){
			imgIcon = new Image("images/icons/ico_info.png");
		} else if(dialogType.equals(GmnDialogType.WARNING)){
			imgIcon = new Image("images/icons/ico_alert.png");
		} else if(dialogType.equals(GmnDialogType.CONFIRM)){
			imgIcon = new Image("images/icons/ico_question.jpg");
		}
		
		init();
	}
	
	public void info() {
		imgIcon = new Image("images/icons/ico_info.png");
		init();
		dialog.center();
	}
	
	private void init() {
		btnOK.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				dialog.hide();
				
				fireEvent(new GmnDialogEvent(DialogActionType.OK));
			}
		});
		
		btnCancel.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				dialog.hide();
				
				fireEvent(new GmnDialogEvent(DialogActionType.CANCEL));
			}
		});
		
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		
		if(dialogType == GmnDialogType.WIDGET) {
			vPanel.add(widget);
		} else {
			HorizontalPanel contentPanel = new HorizontalPanel();
			contentPanel.setSpacing(10);
			imgIcon.setPixelSize(45, 45);
			contentPanel.add(imgIcon);
			contentPanel.add(new HTML(message));
			vPanel.add(contentPanel);
		}
		
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.setWidth("100%");
		buttonPanel.setSpacing(10);
		buttonPanel.add(btnOK);
		
		if(dialogType.equals(GmnDialogType.CONFIRM)) {
			buttonPanel.add(btnCancel);
			buttonPanel.setCellHorizontalAlignment(btnOK, 
					HasHorizontalAlignment.ALIGN_RIGHT);
			buttonPanel.setCellHorizontalAlignment(btnCancel, 
					HasHorizontalAlignment.ALIGN_LEFT);
		} else {
			buttonPanel.setCellHorizontalAlignment(btnOK, 
					HasHorizontalAlignment.ALIGN_CENTER);
		}
		
		vPanel.add(buttonPanel);
		
		dialog.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		dialog.setGlassEnabled(true);
		dialog.setText(title);
		
		dialog.add(vPanel);
		
		dialog.center();
	}
	
}